package com.zswl.decorate.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.zswl.decorate.util.FileUtil;
import com.zswl.decorate.util.ToastUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class SignBoard extends View {

    private Paint mPaint;
    private Path mPath;
    private float mPreX, mPreY;

    public SignBoard(Context context) {
        this(context, null);
    }

    public SignBoard(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SignBoard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(2);
        mPaint.setAntiAlias(true);

        mPath = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        canvas.drawPath(mPath, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath.moveTo(event.getX(), event.getY());
                mPreX = event.getX();
                mPreY = event.getY();
                return true;
            case MotionEvent.ACTION_MOVE:
                float endX = (mPreX + event.getX()) / 2;
                float endY = (mPreY + event.getY()) / 2;
                mPath.quadTo(mPreX, mPreY, endX, endY);
                mPreX = event.getX();
                mPreY = event.getY();
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 保存
     *
     * @return
     */
    public String saveFile() {
        // 控制图片质量

        if (mPath.isEmpty()) {
            return null;
        }


        int quality = 50;

        long currentTime = System.currentTimeMillis();

        File file = new File(FileUtil.getUploadImgPath());

        String mFilePath = file.getAbsolutePath() + File.separator + currentTime;
        try {
            FileOutputStream out = new FileOutputStream(mFilePath);

            Bitmap b = getChartBitmap();
            b.compress(Bitmap.CompressFormat.JPEG, quality, out);

            out.flush();
            out.close();

        } catch (IOException e) {
            e.printStackTrace();

        }
        return mFilePath;
    }

    /**
     * 清除
     */
    public void clearBoard() {
        mPath.reset();
        invalidate();
    }

    public Bitmap getChartBitmap() {
        // 创建一个bitmap 根据我们自定义view的大小
        Bitmap returnedBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
        // 绑定canvas
        Canvas canvas = new Canvas(returnedBitmap);
        // 获取视图的背景
        Drawable bgDrawable = getBackground();
        if (bgDrawable != null)
            // 如果有就绘制
            bgDrawable.draw(canvas);
        else
            // 没有就绘制白色
            canvas.drawColor(Color.WHITE);
        // 绘制
        draw(canvas);
        return returnedBitmap;
    }


}
